package com.ruoyi.phd.service.impl;
import java.io.IOException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.phd.mapper.PhdInformationMapper;
import com.ruoyi.phd.domain.PhdInformation;
import com.ruoyi.phd.service.IPhdInformationService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.transaction.annotation.Transactional;
/**
 * ManageService业务层处理
 * 
 * @author zza
 * @date 2025-09-26
 */
@Service
public class PhdInformationServiceImpl implements IPhdInformationService {
    @Autowired
    private PhdInformationMapper phdInformationMapper;

    /**
     * 查询Manage
     *
     * @param id Manage主键
     * @return Manage
     */
    @Override
    public PhdInformation selectPhdInformationById(Long id) {
        return phdInformationMapper.selectPhdInformationById(id);
    }

    /**
     * 查询Manage列表
     *
     * @param phdInformation Manage
     * @return Manage
     */
    @Override
    public List<PhdInformation> selectPhdInformationList(PhdInformation phdInformation) {
        return phdInformationMapper.selectPhdInformationList(phdInformation);
    }

    /**
     * 新增Manage
     *
     * @param phdInformation Manage
     * @return 结果
     */
    @Override
    public int insertPhdInformation(PhdInformation phdInformation) {
        return phdInformationMapper.insertPhdInformation(phdInformation);
    }

    /**
     * 修改Manage
     *
     * @param phdInformation Manage
     * @return 结果
     */
    @Override
    public int updatePhdInformation(PhdInformation phdInformation) {
        return phdInformationMapper.updatePhdInformation(phdInformation);
    }

    /**
     * 批量删除Manage
     *
     * @param ids 需要删除的Manage主键
     * @return 结果
     */
    @Override
    public int deletePhdInformationByIds(Long[] ids) {
        return phdInformationMapper.deletePhdInformationByIds(ids);
    }

    /**
     * 删除Manage信息
     *
     * @param id Manage主键
     * @return 结果
     */
    @Override
    public int deletePhdInformationById(Long id) {
        return phdInformationMapper.deletePhdInformationById(id);
    }

    /**
     * 批量插入博士信息（实现）
     */
    @Override
    public int batchInsertPhdInformation(List<PhdInformation> phdInformationList) {
        // 调用Mapper层的批量插入方法
        return phdInformationMapper.batchInsertPhdInformation(phdInformationList);
    }

    /**
     * 创建导入模板的工作簿
     *
     * @return 包含模板的Excel工作簿
     */
    @Override
    public Workbook createImportTemplate() {
        // 创建一个新的 Excel 工作簿
        Workbook workbook = new XSSFWorkbook();
        // 创建一个工作表
        Sheet sheet = workbook.createSheet("博士信息模板");

        // 创建表头行
        Row headerRow = sheet.createRow(0);

        // 获取 PhdInformation 类的字段名作为表头，这里简单硬编码，可优化
        String[] headers = {"博士姓名", "所处地区", "学校", "性别", "在读几年级","研究方向" ,"联系方式", "个人主页链接"};
        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
        }

        return workbook;
    }
    @Transactional
    public void reorderIds() {
        List<PhdInformation> list = phdInformationMapper.selectPhdInformationList(null);
        int newId = 1;
        for (PhdInformation info : list) {
            info.setId((long) newId);
            phdInformationMapper.updatePhdInformation(info);
            newId++;
        }
    }
}